package com.mstore.dao;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class SuperDao extends HibernateDaoSupport {

	public Serializable save(Object entity) {
		Serializable id = this.getHibernateTemplate().save(entity);
		return id;
	}
	
	public Object FindById(Class<?> clazz , Serializable id) {
		Object object = this.getHibernateTemplate().get(clazz, id);
		return object;
	}
	
	public void delete(Object entity) {
		this.getHibernateTemplate().delete(entity);
	}
	
	public void update(Object entity) {
		this.getHibernateTemplate().update(entity);
	}
	
	@SuppressWarnings("unchecked")
	public List<Object> getObject(String sql, Object[] values){
		return this.getHibernateTemplate().find(sql, values);
	}
	
	@SuppressWarnings("unchecked")
	public List<Object> find(String hql) {
		return this.getHibernateTemplate().find(hql);
	}
	
	/**
	 * 
	 *分页
	 */
	@SuppressWarnings("unchecked")
	public List<Object> findPage(int index, String bhql){
		final int limit = 14;
		final int start = (index - 1) * limit;
		final String hql = bhql;
		List<Object> list = null;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				List<Object> result = session.createQuery(hql)
						.setFirstResult(start)
						.setMaxResults(limit).list();
				return result;
			}
		});
		return list;
	}
	
	/**
	 * 
	 * 执行原生sql
	 */
	public void executeUpdate(String sql) {
		final String tempsql = sql;
		this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException,SQLException {
				session.createSQLQuery(tempsql).executeUpdate();
				return null;
			}
		});
	}
	
	/**
	 * 
	 * 执行原生sql
	 */
	public Object executeSql(String sql) {
		final String tempsql = sql;
		Object object = this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException,SQLException {
				return session.createSQLQuery(tempsql).list();
			}
		});
		return object;
	}
	
	/**
	 * 行数限制查询
	 */
	@SuppressWarnings("unchecked")
	public List<Object> findByLimit(int index, int limit, String bhql){
		final int finalLimit = limit;
		final int start = index;
		final String hql = bhql;
		List<Object> list = null;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				List<Object> result = session.createQuery(hql)
						.setFirstResult(start)
						.setMaxResults(finalLimit).list();
				return result;
			}
		});
		return list;
	}
	
}
